package com.bookmall.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.bookmall.dao.BookTypeDao;
import com.bookmall.entity.BookType;
import com.bookmall.util.DBUtil;

public class BookTypeDaoImpl implements BookTypeDao {

	@Override
	public boolean insert(BookType t) throws SQLException {
		Connection c = DBUtil.getConnection();
		String sql = "insert into book_type_tb(typeName,parentId) values(?,?) ";
		PreparedStatement ps = c.prepareStatement(sql);
		ps.setString(1, t.getTypeName());
		ps.setInt(2, t.getParentId());
		int row = ps.executeUpdate();
		DBUtil.closeConnection(c, ps, null);
		return row == 1 ? true : false;
	}

	@Override
	public boolean update(BookType t) throws SQLException {
		Connection c = DBUtil.getConnection();
		String sql = "update book_type_tb set typeName = ?,parentId = ? where typeId = ?";
		PreparedStatement ps = c.prepareStatement(sql);
		ps.setString(1, t.getTypeName());
		ps.setInt(2, t.getParentId());
		ps.setInt(3, t.getTypeId());
		int row = ps.executeUpdate();
		DBUtil.closeConnection(c, ps, null);
		return row == 1 ? true : false;
	}

	@Override
	public boolean delete(int id) throws SQLException {
		Connection c = DBUtil.getConnection();
		String sql = "update book_type_tb set delete_flag = 1 where typeId = ? and delete_flag = 0";
		PreparedStatement ps = c.prepareStatement(sql);
		ps.setInt(1, id);
		int row = ps.executeUpdate();
		DBUtil.closeConnection(c, ps, null);
		return row == 1 ? true : false;
	}

	@Override
	public BookType selectById(int id) throws SQLException {
		Connection c = DBUtil.getConnection();
		String sql = "select typeId,typeName,parentId,create_time,modify_time from book_type_tb where typeId =? and delete_flag=0";
		PreparedStatement ps = c.prepareStatement(sql);
		ps.setInt(1, id);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			BookType a = new BookType();
			a.setTypeId(rs.getInt(1));
			a.setTypeName(rs.getString(2));
			a.setParentId(rs.getInt(3));
			a.setCreateTime(rs.getTimestamp(4).toLocalDateTime());
			if (rs.getTimestamp(5) != null) {
				a.setModifyTime(rs.getTimestamp(5).toLocalDateTime());
			}
			DBUtil.closeConnection(c, ps, rs);
			return a;
		} else {
			DBUtil.closeConnection(c, ps, rs);
			return null;
		}

	}

	@Override
	public List<BookType> selectByPage(int page, int pageSize) throws SQLException {
		Connection c = DBUtil.getConnection();
		List<BookType> booktype = new ArrayList<BookType>();
		String sql = "select typeId,typeName,parentId,create_time,modify_time from book_type_tb where delete_flag=0 limit ?,?";
		PreparedStatement ps = c.prepareStatement(sql);
		ps.setInt(1, (page - 1) * pageSize);
		ps.setInt(2, pageSize);
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			BookType a = new BookType();
			a.setTypeId(rs.getInt(1));
			a.setTypeName(rs.getString(2));
			a.setParentId(rs.getInt(3));
			a.setCreateTime(rs.getTimestamp(4).toLocalDateTime());
			if (rs.getTimestamp(5) != null) {
				a.setModifyTime(rs.getTimestamp(5).toLocalDateTime());
			}
			booktype.add(a);
		}
		DBUtil.closeConnection(c, ps, rs);
		return booktype.isEmpty() ? Collections.emptyList() : booktype;
	}

	@Override
	public List<BookType> selectByIds(List<Integer> ids, int page, int pageSize) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int deleteByIds(List<Integer> ids) throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<BookType> selectByParentId(int parentId, int page, int pageSize) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<BookType> selectByParentTypeName(String name, int page, int pageSize) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

}
